home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / tex / files / !tex / TeXsource / commontex / c / print < prev    next >
Encoding:
Text File  |  1988-04-18  |  7.3 KB  |  333 lines

  1. /*
  2.  *    Copyright 1986, 1987 Pat Joseph Monardo. All rights reserved.
  3.  *    Copying of this file is granted according to the provisions 
  4.  *    specified in the file COPYING which must accompany this file.
  5.  */
  6.  
  7.  
  8. /*
  9.  *              print.c
  10.  */
  11.  
  12. #include "tex.h"
  13. #include "texext.h"
  14. #include "eq.h"
  15. #include "char.h"
  16. #include "str.h"
  17. #include "io.h"
  18. #include "print.h"
  19.  
  20. alpha_file      log_file;
  21.  
  22. char    dig[23];
  23. int             file_offset;
  24. val             first_count;
  25. int             selector = TERM_ONLY;
  26. val             tally;
  27. int             term_offset;
  28. ascii   trick_buf[ERROR_LINE];
  29. val             trick_count;
  30.  
  31. print_ln ()
  32. {
  33.         switch (selector)
  34.         {
  35.         case TERM_AND_LOG:
  36.                 wterm_cr();
  37.                 term_offset = 0;
  38.                 wlog_cr();
  39.                 file_offset = 0;
  40.                 break;
  41.  
  42.         case LOG_ONLY:
  43.                 wlog_cr();
  44.                 file_offset = 0;
  45.                 break;
  46.  
  47.         case TERM_ONLY:
  48.                 wterm_cr();
  49.                 term_offset = 0;
  50.                 break;
  51.  
  52.         case NO_PRINT:
  53.         case PSEUDO:
  54.         case NEW_STRING:
  55.                 break;
  56.  
  57.         default:
  58.                 wfile_cr();
  59.                 break;
  60.         }
  61. }       
  62.  
  63. print_char (c)
  64.         ascii   c;
  65. {
  66.         if (c == new_line_char && selector < PSEUDO) {
  67.                 print_ln();
  68.                 return;
  69.         }
  70.         switch (selector)
  71.         {
  72.         case TERM_AND_LOG:
  73.                 wterm(xchr[c]);
  74.                 incr(term_offset);
  75.                 wlog(xchr[c]);
  76.                 incr(file_offset);
  77.                 if (term_offset == MAX_PRINT_LINE) {
  78.                         wterm_cr();
  79.                         term_offset = 0;
  80.                 }
  81.                 if (file_offset == MAX_PRINT_LINE) {
  82.                         wlog_cr();
  83.                         file_offset = 0;
  84.                 }
  85.                 break;
  86.  
  87.         case LOG_ONLY:
  88.                 wlog(xchr[c]);
  89.                 incr(file_offset);
  90.                 if (file_offset == MAX_PRINT_LINE)
  91.                         print_ln();
  92.                 break;
  93.  
  94.         case TERM_ONLY:
  95.                 wterm(xchr[c]);
  96.                 incr(term_offset);
  97.                 if (term_offset == MAX_PRINT_LINE)
  98.                         print_ln();
  99.                 break;
  100.  
  101.         case NO_PRINT:
  102.                 return;
  103.  
  104.         case PSEUDO:
  105.                 if (tally < trick_count) 
  106.                         trick_buf[tally % ERROR_LINE] = c;
  107.                 break;
  108.  
  109.         case NEW_STRING:
  110.                 if (pool_ptr < POOL_SIZE) 
  111.                         append_char(c);
  112.                 break;
  113.  
  114.         default:
  115.                 wfile(xchr[c]);
  116.                 break;
  117.         }
  118.         incr(tally);
  119. }
  120.  
  121. print_ASCII (c)
  122.         int              c;
  123. {
  124.         if (c >= 0 && c <= 127)
  125.                 print_str(c);
  126.         else {
  127.                 print_char('[');
  128.                 if (c < 0)
  129.                         print_int(c);
  130.                 else print_hex((val) c);
  131.                 print_char(']');
  132.         }
  133. }
  134.  
  135. print_str (s)
  136.         str             s;
  137. {
  138.         int             j;
  139.  
  140.         if (s >= str_ptr)
  141.                 s = make_string_given("???");
  142.         else if (s < 128) {
  143.                 if (s < 0)
  144.                         s = make_string_given("???");
  145.                 else if (s == new_line_char && selector < PSEUDO) {
  146.                         print_ln();
  147.                         return;
  148.                 }
  149.         }
  150.         j = str_start[s];
  151.         while (j < str_start[s + 1]) {
  152.                 print_char(str_pool[j]);
  153.                 incr(j);
  154.         }
  155. }
  156.  
  157. slow_print (s)
  158.         str             s;
  159. {
  160.         int             j;
  161.  
  162.         if (s >= str_ptr)
  163.                 s = make_string_given("???");
  164.         else if (s < 128) {
  165.                 if (s < 0)
  166.                         s = make_string_given("???");
  167.                 else if (s == new_line_char && selector < PSEUDO) {
  168.                         print_ln();
  169.                         return;
  170.                 }
  171.         }
  172.         j = str_start[s];
  173.         while (j < str_start[s + 1]) {
  174.                 print_str(str_pool[j]);
  175.                 incr(j);
  176.         }
  177. }
  178.  
  179. print (s)
  180.         char*   s;
  181. {
  182.         while (*s) { 
  183.                 print_char(*s);
  184.                 incr(s);
  185.         }
  186. }
  187.  
  188. print_nl (s)
  189.         char*   s;
  190. {
  191.         if (term_offset > 0 && odd(selector) ||
  192.                 file_offset > 0 && selector >= LOG_ONLY)
  193.                 print_ln();
  194.         print(s);
  195. }
  196.  
  197. print_esc (s)
  198.         char*   s;
  199. {
  200.         ascii   c;
  201.  
  202.         c = escape_char;
  203.         if (c >= 0 && c < 128) 
  204.                 print_char(c);
  205.         print(s);
  206. }
  207.  
  208. print_int (n)
  209.         int             n;
  210. {
  211.         int             k;
  212.         int             m;
  213.  
  214.         k = 0;
  215.         if (n < 0)  {
  216.                 print_char('-');
  217.                 negate(n);
  218.         }
  219.         do {
  220.                 dig[k] = n % 10;
  221.                 n /= 10;
  222.                 incr(k);
  223.         } while (n != 0);
  224.         print_the_digs(k);
  225. }
  226.  
  227. print_val (n)
  228.         val             n;
  229. {
  230.         int             k;
  231.         val             m;
  232.  
  233.         k = 0;
  234.         if (n < 0)  {
  235.                 print_char('-');
  236.                 if (n > -100000000)
  237.                         negate(n);
  238.                 else {
  239.                         m = -1 - n;
  240.                         n = m / 10;
  241.                         m = m % 10 + 1;
  242.                         k = 1;
  243.                         if (m < 10)
  244.                                 dig[0] = m;
  245.                         else {
  246.                                 dig[0] = 0;
  247.                                 incr(n);
  248.                         }
  249.                 }
  250.         }
  251.         do {
  252.                 dig[k] = n % 10;
  253.                 n /= 10;
  254.                 incr(k);
  255.         } while (n != 0);
  256.         print_the_digs(k);
  257. }
  258.  
  259. print_hex (v)
  260.         val             v;
  261. {
  262.         int             k;
  263.  
  264.         k = 0;
  265.         print_char('"');
  266.         do {
  267.                 dig[k] = v&15;
  268.                 v >>= 4;
  269.                 incr(k);
  270.         } while (v != 0);
  271.         print_the_digs(k);
  272. }
  273.  
  274. print_the_digs (k)
  275.         int              k;
  276. {
  277.         while (k > 0) {
  278.                 decr(k);
  279.                 if (dig[k] < 10)
  280.                         print_char('0' + dig[k]);
  281.                 else print_char('A' - 10 + dig[k]);
  282.         }
  283. }
  284.  
  285. print_two (n)
  286.         int             n;
  287. {
  288.         n = abs(n) % 100;
  289.         print_char('0' + n / 10);
  290.         print_char('0' + n % 10);
  291. }
  292.  
  293. print_roman_int (n)
  294.         val             n;
  295. {
  296.         char*   j = "m2d5c2l5x2v5i";
  297.         char*   k;
  298.         val             u;
  299.         val             v = 1000;
  300.  
  301.         loop {
  302.                 while (n >= v) {
  303.                         print_char(*j);
  304.                         n -= v;
  305.                 }
  306.                 if (n <= 0) return; /* nonpositive input produces no output */
  307.                 k = j + 2;
  308.                 u = v / (*(k - 1) - '0');
  309.                 if (*(k - 1) == '2')  {
  310.                         k += 2;
  311.                         u /= *(k - 1) - '0';
  312.                 }
  313.                 if (n + u >= v)  {
  314.                         print_char(*k);
  315.                         n += u;
  316.                 } else {
  317.                         j += 2;
  318.                         v /= *(j - 1) - '0';
  319.                 }
  320.         }
  321. }
  322.  
  323. print_current_string ()
  324. {
  325.         int             j;
  326.  
  327.         j = str_start[str_ptr];
  328.         while (j < pool_ptr) {
  329.                 print_char(str_pool[j]);
  330.                 incr(j);
  331.         }
  332. }
  333.